package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import po.City;
import service.CityService;

/**
 * Create By IntelliJ IDEA
 *
 * @author: jsonor
 * @create-Time: 2017/10/19 9:12
 * @description: ${description}
 */
@WebServlet(name = "addServlet", urlPatterns = {"/add"})
public class AddServlet extends HttpServlet {

  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    // 1. 获取所有的数据
    String name = request.getParameter("name");
    String countryCode = request.getParameter("countryCode");
    String district = request.getParameter("district");
    int population = Integer.parseInt(request.getParameter("population"));

    try {
      CityService service = new CityService();
      City city = new City(null, name, countryCode, district, population);
      service.add(city);
      response.sendRedirect(request.getContextPath() + "/display");
    } catch (Exception e) {
      request.setAttribute("error", e);
      request.getRequestDispatcher("/error.jsp").forward(request, response);
    }
  }
}
